/**
 * Copyright 2009 University of  Washington
 * This code is released under the GPL version 3 - with a copy of the license included
 * @author Maya Hao Li
 *
 */
package seedpod.webapp.view.seedpodwidgets;

/**
 * EMS.View.CollectionWriter
 * Writes all the instances of a classType
 */

import seedpod.model.SeedpodDO;
import seedpod.webapp.view.htmlwidget.HtmlLink;
import seedpod.webapp.view.htmlwidget.Table;
import seedpod.webapp.view.htmlwidget.TextButton;


public class InstanceCollectionRenderer
    extends DefaultSeedpodInstanceRenderer
{
  private int _collectionSize = 0;
  private int _initialIndex = 0;
  private int _sizePerCollection = 50;  

  public InstanceCollectionRenderer ( String objectType )
  {
    super (objectType);
  }


  public String render ()
  {
    Table col = new Table();

    col.setAlternativeRowColor( false );
    col.setTableCellSpacing(1, 3 );

    col.addRow();
    col.addCell( makeHeader() );

    col.addRow();
    col.addCell( "(Class = " + objectType + ")");

    col.addRow();
    col.addCell( renderInstances() );

    col.addRow();
    // footer.setRowColor("#adadad");
    col.addCell( makeFooter() );
    return col.render();
  }

  public String makeFooter()
  {
    String footerTxt = "";

    String lnkPrevious = "#"; /* EMS.Controller.
        ShowInstanceCollection.
        constructURL( objectType,
                      ( _initialIndex - _sizePerCollection ) ); */
    String lnkNext = "#"; /* EMS.Controller.
        ShowInstanceCollection.
        constructURL( objectType,
                      ( _initialIndex + _sizePerCollection ) ); */
    TextButton btnPrevious, btnNext;

    if ( _initialIndex > 0 )
    {
      btnPrevious = new TextButton( "previousPage", "<= Previous ", lnkPrevious );
      footerTxt += btnPrevious.render();
    }
    else
    {
      footerTxt += "<=Previous";
    }

    footerTxt += " | " + (_initialIndex+1) + " - ";
       footerTxt += ((_initialIndex + _sizePerCollection ) > _collectionSize ) ?
           _collectionSize : (_initialIndex + _sizePerCollection) ;
       footerTxt += " of " + _collectionSize;

    footerTxt += "|";

    if (( _initialIndex + _sizePerCollection ) < _collectionSize  )
    {
      btnNext = new TextButton( "nextPage", " Next =>", lnkNext);
      footerTxt += btnNext.render();
    }
    else
    {
      footerTxt += " Next =>";
    }


    return footerTxt;
  }

  private String renderInstances()
  {
    Table content = new Table( "instancesCollectionTable");
    content.setTableCellSpacing( 1, 4 );
    content.setAlternativeRowColor( true );

    for ( int i = 0; i < numComponents; i++ )
    {
      content.addRow();
      content.addCell( Integer.toString(_initialIndex+i+1));
      // content.addCellComponent( icon );
      content.addCell( objComponents.get( i ) );
    }

    return content.render();
  }

  public void setCollectionParameter( int initIndex, int colSize, int limit)
  {
    _initialIndex = initIndex;
    _sizePerCollection = limit;
    _collectionSize = colSize;
  }

  public void addComponent ( SeedpodDO obj )
  {
    String url = "#"; /* EMS.Controller.DisplayInstance.constructUrl(obj.getOid(), obj.getClsName()); */
    objComponents.add( new HtmlLink( url, obj.getLabel() ) );
    numComponents++;
  }
}